#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int no;
    int score;
} Student;

typedef struct LNode {
    Student *data;
    struct LNode *next;
} LNode, *LinkList;

void initLinkList(LinkList &head);

void insertLinkList(LinkList &head, Student (&stus)[20]);

void listInsert(LinkList &head, int i, Student &s);

void deleteLinkList(LinkList &head, int i);

void show(LinkList &head);

int main() {
    Student stus[20];
    for (int i = 0; i < 20; i++) {
        stus[i].no = i + 43;
        stus[i].score = i * 10;
    }
    LinkList head;
    initLinkList(head);
    insertLinkList(head, stus);
    show(head);
    printf("================================================\n");
    listInsert(head, 4, stus[0]);
    show(head);
    printf("================================================\n");
    deleteLinkList(head, 4);
    show(head);
    printf("================================================\n");


    system("pause");
    return 0;
}

// 初始化链表
void initLinkList(LinkList &head) {
    head = new LNode;
    head->next = NULL;
}

// 前插方式插入20个数据
void insertLinkList(LinkList &head, Student (&stus)[20]) {
    LinkList p = head;
    for (int i = 0; i < 20; i++) {
        LinkList p2 = new LNode;
        p2->data = &stus[i];
        p2->next = NULL;
        if (p->next) {
            p2->next = p->next;
            p->next = p2;
        } else {
            p->next = p2;
        }
    }

}

// 向第i个位置插入数据
void listInsert(LinkList &head, int i, Student &s) {
    LinkList p = head;
    for (int j = 0; j < i; j++) {
        p = p->next;
    }

    LinkList p2 = new LNode;
    p2->data = &s;
    p2->next = p->next;
    p->next = p2;
}

// 删除第i个元素
void deleteLinkList(LinkList &head, int i) {
    LinkList p = head;
    for (int j = 0; j < i; j++) {
        p = p->next;
    }
    if (p->next) {
        p->next = p->next->next;
    } else {
        p->next = NULL;
    }

}

// 展示链表
void show(LinkList &head) {
    LinkList p = head;
    while (p->next) {
        p = p->next;
        printf("no: %d, score: %d \n", p->data->no, p->data->score);
    }

}
